package cn.ad.server.persistence.typehandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.ad.server.domain.enumration.ResourceType;

/**
 * ResourceTypeEnum 映射handler
 * @author wangwei
 *
 */
public class ResourceTypeEnumStatusTypeHandler extends BaseTypeHandler<ResourceType> {
	private static Logger logger = LoggerFactory.getLogger(ResourceTypeEnumStatusTypeHandler.class);

    public ResourceTypeEnumStatusTypeHandler(Class<ResourceType> type) {
    }


    @Override
    public ResourceType getNullableResult(ResultSet rs, String columnName) throws SQLException {
    	String i = rs.getString(columnName);
        if (rs.wasNull()) {
            return null;
        } else {
        	return getObject(i);
        }
    }
    
    /**
     * 从数据库获得值映射为Enum
     * @param key
     * @return
     */
    private ResourceType getObject(String key){
    	return ResourceType.resourceTypeMap.get(key);
    }
    

    @Override
    public ResourceType getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
    	String i = rs.getString(columnIndex);
        if (rs.wasNull()) {
            return null;
        } else {
        	return getObject(i);
        }
    }

    @Override
    public ResourceType getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
    	 return null;
    }

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, ResourceType parameter, JdbcType jdbcType)
			throws SQLException {
		// TODO 待写
		
	}

}
